let extend = function(a, b) {
		a = a || {};
		for (let n in b) a[n] = b[n];
		return a;
	},
	checkTime = function(i) {
		if (i < 10) {
			i = "0" + i;
		}
		return i;
	},
	hasOwn = {}.hasOwnProperty;

extend(window, {
	Cookie() {
		function getsec(str) {
			var str1 = str.substring(1, str.length) * 1;
			var str2 = str.substring(0, 1);
			if (str2 == "s") {
				return str1 * 1000;
			} else if (str2 == "h") {
				return str1 * 60 * 60 * 1000;
			} else if (str2 == "d") {
				return str1 * 24 * 60 * 60 * 1000;
			}
		}
		return {
			set: function(name, value, time, path, domain) {
				var strsec = getsec(time);
				var exp = new Date();
				exp.setTime(exp.getTime() + strsec * 1);
				document.cookie = name + "=" + value + ";expires=" + exp.toGMTString()+";path="+(path||"/")+";domain="+(domain||location.host.replace("www.",""));
			},
			del: function(name) {
				this.set(name, '', 's-1', "/", location.host.replace("www.",""));
			},
			get: function(name) {
				var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
				if (arr = document.cookie.match(reg)){
					return (arr[2]);
				}else{
					return null;
				}
			}
		}
	},
	isPlainObject(obj) {
		var key;
		if (!obj || typeof obj !== "object" || obj.nodeType) {
			return false;
		}
		try {
			if (obj.constructor &&
				!hasOwn.call(obj, "constructor") &&
				!hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
				return false;
			}
		} catch (e) {
			return false;
		}
		for (key in obj) {}
		return key === undefined || hasOwn.call(obj, key);
	},
	isFunction(obj) {
		return typeof obj === "function" && typeof obj.nodeType !== "number";
	},
	throttle(func, wait, options) {
		/* options的默认值
		 *  表示首次调用返回值方法时，会马上调用func；否则仅会记录当前时刻，当第二次调用的时间间隔超过wait时，才调用func。
		 *  options.leading = true;
		 * 表示当调用方法时，未到达wait指定的时间间隔，则启动计时器延迟调用func函数，若后续在既未达到wait指定的时间间隔和func函数又未被调用的情况下调用返回值方法，则被调用请求将被丢弃。
		 *  options.trailing = true; 
		 * 注意：当options.trailing = false时，效果与上面的简单实现效果相同
		 */
		var context, args, result;
		var timeout = null;
		var previous = 0;
		if (!options) options = {};
		var later = function() {
			previous = options.leading === false ? 0 : new Date();
			timeout = null;
			result = func.apply(context, args);
			if (!timeout) context = args = null;
		};
		return function() {
			var now = new Date();
			if (!previous && options.leading === false) previous = now;
			// 计算剩余时间
			var remaining = wait - (now - previous);
			context = this;
			args = arguments;
			// 当到达wait指定的时间间隔，则调用func函数
			// 精彩之处：按理来说remaining <= 0已经足够证明已经到达wait的时间间隔，但这里还考虑到假如客户端修改了系统时间则马上执行func函数。
			if (remaining <= 0 || remaining > wait) {
				// 由于setTimeout存在最小时间精度问题，因此会存在到达wait的时间间隔，但之前设置的setTimeout操作还没被执行，因此为保险起见，这里先清理setTimeout操作
				if (timeout) {
					clearTimeout(timeout);
					timeout = null;
				}
				previous = now;
				result = func.apply(context, args);
				if (!timeout) context = args = null;
			} else if (!timeout && options.trailing !== false) {
				// options.trailing=true时，延时执行func函数
				timeout = setTimeout(later, remaining);
			}
			return result;
		};
	},
	getScrollTop() {
		var scrollTop = window.pageYOffset ||
			document.documentElement.scrollTop ||
			document.body.scrollTop ||
			0;
		return scrollTop;
	},
	dateFormat(d, pattern) {
		var d = new Date(parseInt(d));
		var year = d.getFullYear();
		var month = d.getMonth() + 1;
		var date = d.getDate();
		var hour = checkTime(d.getHours());
		var minute = checkTime(d.getMinutes());
		var second = checkTime(d.getSeconds());
		return year + "-" + (month < 10 ? "0" + month : month) + "-" + (date < 10 ? "0" + date : date);
	},
	extend(a, b) {
		return extend(a, b)
	},
	stringToJson:function(str, fn){
		let a = {};
		str = decodeURIComponent(str).split(',');
		for (var i=0;i<str.length;i++){
			let b = str[i].split('=');
			a[b[0]] = b[1];
		}
		fn(a);
		return a;
	}
});

var ua = navigator.userAgent.toLowerCase(),
	device = {
		os: {
			version: 0,
			isIOS: ua.indexOf("iphone") > -1 || ua.indexOf("ipad") > -1 || ua.indexOf("ios") > -1,
			isAndroid: ua.indexOf("android") > -1 || ua.indexOf("adr") > -1 || ua.indexOf("linux;") > -1
		},
		browser: {
			version: 0,
			isQQ: ua.indexOf("qq/") > -1,
			isqqbrowser: ua.indexOf("mqqbrowser/") > -1,
			isUC: ua.indexOf("ucbrowser/") > -1 || ua.indexOf("ucweb/") > -1,
			isWechat: ua.indexOf("micromessenger/") > -1,
			isSamsung: ua.indexOf("samsungbrowser/") > -1,
			isSogou: ua.indexOf("sogoumobilebrowser/") > -1,
			isPinganWifi: ua.indexOf("pawifi") > -1,
			isChrome: ua.indexOf('chrome') > -1,
			isOpera: ua.indexOf('opera') > -1 || ua.indexOf('opr') > -1,
			isFirefox: ua.indexOf('firefox') > -1 || ua.indexOf('fxios') > -1,
			isBaiduboxapp: ua.indexOf('baiduboxapp/') > -1,
			isBaidubrowser: ua.indexOf('baidubrowser/') > -1,
			isQihoobrowser: ua.indexOf('qihoobrowser/') > -1,
			isMxios: ua.indexOf('mxios/') > -1,
			isTimMobile: ua.indexOf('tim/') > -1,
			isHXApp: ua.indexOf('hxappversion') > -1 || ua.indexOf('hxapp') > -1,
			isWeiBo: ua.indexOf('weibo') > -1,
			isMiuiBrowser: ua.indexOf('miuibrowser/') > -1
		},
		model: {
			isIphoneX: /iphone[\s\S]*os x/.test(ua) && screen.height === 812 && screen.width == 375,
			isHUAWEI: /huawei/.test(ua),
			isOPPO: /oppo/.test(ua),
			isMEIZU: /meizu/.test(ua),
			isXIAOMI: /xiaomi/.test(ua) || /miuibrowser\//.test(ua) || /mi/.test(ua),
			isVIVO: /vivo/.test(ua),
			isREDMI: /redmi/.test(ua),
			isHONORBLN: /honorbln/.test(ua),
			isSAMSUNG: /sm\-/.test(ua) || /samsung/.test(ua),
			isLE: /Le\s+/.test(ua),
			isONEPLUS: /oneplus/.test(ua),
			isDOOV: /doov/.test(ua),
			isNBY: /nx[0-9]+/.test(ua),
			isLG: /lg\-/.test(ua),
			isOD: /od[0-9]+/.test(ua),
			isANE: /ane\-/.test(ua),
			isZUK: /zuk\s+/.test(ua),
			isLenovo: /lenovo/.test(ua)
		}
	};
device.browser.isSafari = device.os.isiOS && ua.indexOf("safari/") > -1 && !device.browser.isqqbrowser;
device.browser.isIApp = device.os.isiOS && !device.browser.isSafari && !device.browser.isqqbrowser && !device.browser.isUC && !device.browser.isWechat && !device.browser.isSamsung && !device.browser.isSogou && !device.browser.isPinganWifi;

extend(window, {
	device: device
})


String.prototype.tmpl = function(v) {
	let a = this;
	for (let n in v) {
		let r = new RegExp("\\$\\{" + n + "\\}", "gim");
		a = a.replace(r, v[n])
	}
	return a;
}
Array.prototype.each = function(a) {
	let b = this;
	for (let i = 0; i < b.length; i++) {
		let r = a.call(b[i], i, b[i]);
		if (r === false) break;
	}
	return b;
}